﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace Zeus.DAL.ADO.MySql
{
    public class MySqlDb : IDisposable
    {
        public MySqlConnection Connection { get; set; }
        public String ConnectionString { get; set; }

        public MySqlDb(string pStr)
        {
            ConnectionString = ConfigurationManager.ConnectionStrings[pStr].ConnectionString;
        }

        ~MySqlDb()
        {
            try
            {
                if (Connection != null)
                    Connection.Close();
            }
            catch { }
            try
            {
                Dispose();
            }
            catch { }
        }

        /// ***********************************
        /// Open Connection
        /// ***********************************
        public void Open()
        {
            if (Connection == null)
            {
                Connection = new MySqlConnection(ConnectionString);
                //Connection.ConnectionTimeout = 0;
            }
            if (Connection.State.Equals(ConnectionState.Closed))
            {
                Connection.Open();
            }
        }

        /// ***********************************
        /// Close Connection
        /// ***********************************
        public void Close()
        {
            if (Connection != null)
                Connection.Close();
        }

        /// ***********************************
        /// Dispose Connection
        /// ***********************************
        public void Dispose()
        {
            // Ensure Connection Closed
            if (Connection != null)
            {
                Connection.Dispose();
                Connection = null;
            }
        }

        /// **********************************************************************
        /// Sql Executor
        /// <param name="SqlString">Sql语句</param>
        /// <returns>
        /// 对Update、Insert、Delete为影响到的行数，其他情况为-1
        /// </returns>
        /// **********************************************************************
        public int ExecuteSQL(String SqlString)
        {
            int count = -1;
            Open();
            try
            {
                MySqlCommand cmd = new MySqlCommand(SqlString, Connection);
                count = cmd.ExecuteNonQuery();
            }
            catch
            {
                count = -1;
            }
            finally
            {
                Close();
            }

            return count;
        }

        /// **********************************************************************
        /// Sql Executor
        /// <param name="SqlString">Sql语句</param>
        /// <returns>
        /// 对Update、Insert、Delete为影响到的行数，其他情况为-1
        /// </returns>
        /// **********************************************************************
        public int ExecuteSqlParam(String SqlString, MySqlParameter[] Prams)
        {
            int count = -1;
            Open();
            try
            {
                MySqlCommand cmd = new MySqlCommand(SqlString, Connection);
                foreach (MySqlParameter Param in Prams)
                {
                    cmd.Parameters.Add(Param);
                }
                count = cmd.ExecuteNonQuery();
            }
            catch
            {
                count = -1;
            }
            finally
            {
                Close();
            }

            return count;
        }

        /// **************************************************
        /// 获取数据:返回一个SqlDataReader 
        /// （调用后主意调用SqlDataReader.Close()）
        /// <param name="SqlString">Sql语句</param>
        /// <returns>SqlDataReader</returns>
        /// **************************************************
        public MySqlDataReader GetDataReader(String SqlString)
        {
            Open();
            MySqlCommand cmd = new MySqlCommand(SqlString, Connection);

            return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
        }

        /// **************************************************
        /// 获取数据:返回一个SqlDataReader 
        /// （调用后主意调用SqlDataReader.Close()）
        /// <param name="SqlString">Sql语句</param>
        /// <returns>SqlDataReader</returns>
        /// **************************************************
        public MySqlDataReader GetDataReaderParam(String SqlString, MySqlParameter[] Prams)
        {
            Open();
            MySqlCommand cmd = new MySqlCommand(SqlString, Connection);
            foreach (MySqlParameter Param in Prams)
            {
                cmd.Parameters.Add(Param);
            }

            return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
        }
    }
}
